package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "充值管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class RechargeInfoApiController {

    @Autowired
    private RechargeInfoService rechargeInfoService;


    /**
     * 根据充值订单编号查询充值信息
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号查询充值信息")
    @GetMapping("/rechargeInfo/getRechargeInfo/{orderNo}")
    public Result<RechargeInfo> getRechargeInfo(@PathVariable String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoService.getOne(
                new LambdaQueryWrapper<RechargeInfo>()
                        .eq(RechargeInfo::getOrderNo, orderNo)
        );
        return Result.ok(rechargeInfo);
    }


    /**
     * 充值-新增充值记录
     *
     * @param rechargeInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "充值-新增充值记录")
    @PostMapping("/rechargeInfo/submitRecharge")
    public Result<Map<String, String>> submitRecharge(@RequestBody RechargeInfoVo rechargeInfoVo) {
        Map<String, String> map = rechargeInfoService.submitRecharge(rechargeInfoVo);
        return Result.ok(map);
    }


    /**
     * 充值-充值成功
     * @param orderNo
     * @return
     */
    @Operation(summary = "充值-充值成功")
    @GetMapping("/rechargeInfo/rechargePaySuccess/{orderNo}")
    public Result rechargePaySuccess(@PathVariable String orderNo) {
        rechargeInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }

    /**
     * 分页查询充值记录
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页查询充值记录")
    @GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Long page,
                                                               @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> pageParam = new Page<>(page, limit);
        rechargeInfoService.getUserAccountDetailPage(pageParam, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, userId);
        return Result.ok(pageParam);
    }


    /**
     * 分页查询消费记录
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页查询消费记录")
    @GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserConsumePage(@PathVariable Long page,
                                                                @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> pageParam = new Page<>(page, limit);
        rechargeInfoService.getUserAccountDetailPage(pageParam, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS, userId);
        return Result.ok(pageParam);
    }
}

